package com.liwenfeng.tcpandupd;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

//UPD通信
public class UpdClient {
    public static void main(String[] args){
        //创建客户端对象，如不指定端口号，会随机分配一个
        try (DatagramSocket socket = new DatagramSocket();){
            Scanner sc = new Scanner(System.in);
            while (true){
                System.out.println("请说：");
                String msg = sc.nextLine();
                if (msg.equals("exit")) break;
                byte[] bytes = msg.getBytes();
                //创建数据包对象封装要发出去的数据
                DatagramPacket packet = new DatagramPacket(bytes,bytes.length, InetAddress.getLocalHost(),8888);
                //开始发送数据包
                socket.send(packet);
                System.out.println("数据已发出...");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
